package com.yinhe.module.lys.security.controller;

import com.yinhe.common.annotation.Log;
import com.yinhe.common.core.controller.BaseController;
import com.yinhe.common.core.domain.AjaxResult;
import com.yinhe.common.core.domain.entity.SysUser;
import com.yinhe.common.core.domain.model.LoginUser;
import com.yinhe.common.core.page.TableDataInfo;
import com.yinhe.common.enums.BusinessType;
import com.yinhe.common.utils.poi.ExcelUtil;
import com.yinhe.system.domain.YhRiskProject;
import com.yinhe.system.domain.YhSecuritywork;
import com.yinhe.system.service.IYhSecurityworkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/lys/security")
public class YhSecurityController extends BaseController
{
    @Autowired
    private IYhSecurityworkService yhSecurityworkService;
    /**
     * 导入保卫工作列表
     */
    @Log(title = "保卫工作", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('lys:security:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<YhSecuritywork> util = new ExcelUtil<YhSecuritywork>(YhSecuritywork.class);
        List<YhSecuritywork> SecurityworkList = util.importExcel(file.getInputStream());
        String message = yhSecurityworkService.importSecuritywork(SecurityworkList, updateSupport);
        return AjaxResult.success(message);
    }
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<YhSecuritywork> util = new ExcelUtil<YhSecuritywork>(YhSecuritywork.class);
        util.importTemplateExcel(response, "保卫工作数据");
    }
    /**
     * 查询保卫工作列表
     */
    @PreAuthorize("@ss.hasPermi('lys:security:list')")
    @GetMapping("/list")
    public TableDataInfo list(YhSecuritywork yhSecuritywork)
    {
        startPage();
        List<YhSecuritywork> list = yhSecurityworkService.selectYhSecurityworkList(yhSecuritywork);
        System.out.println(yhSecuritywork.getRepresentativeOffice()+"8");
        System.out.println(yhSecuritywork.getSecurityProject()+"9");
        return getDataTable(list);
    }

    /**
     * 导出保卫工作列表
     */
    @PreAuthorize("@ss.hasPermi('lys:security:export')")
    @Log(title = "保卫工作", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, YhSecuritywork yhSecuritywork)
    {
        List<YhSecuritywork> list = yhSecurityworkService.selectYhSecurityworkList(yhSecuritywork);
        ExcelUtil<YhSecuritywork> util = new ExcelUtil<YhSecuritywork>(YhSecuritywork.class);
        util.hideColumn("userId","deptId","note",
                "note2","note1",
                "note3","note4","note5","note6","note7","note8","note9","note10");
        util.exportExcel(response, list, "保卫工作数据");


    }

    /**
     * 获取保卫工作详细信息
     */
    @PreAuthorize("@ss.hasPermi('lys:security:query')OR @ss.hasPermi('lys:branch:edit')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(yhSecurityworkService.selectYhSecurityworkById(id));
    }

    /**
     * 新增保卫工作
     */
    @PreAuthorize("@ss.hasPermi('lys:security:add')")
    @Log(title = "保卫工作", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody YhSecuritywork yhSecuritywork)
    {
        // 获取当前登录用户
        LoginUser loginUser = getLoginUser();
        SysUser sysUser = loginUser.getUser();
        // 存入userId和deptId，做数据权限
        yhSecuritywork.setUserId(sysUser.getUserId());
        yhSecuritywork.setDeptId(Long.valueOf("101"));

        return toAjax(yhSecurityworkService.insertYhSecuritywork(yhSecuritywork));
    }

    /**
     * 修改保卫工作
     */
    @PreAuthorize("@ss.hasPermi('lys:security:edit')OR @ss.hasPermi('lys:branch:query')")
    @Log(title = "保卫工作", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YhSecuritywork yhSecuritywork)
    {

        return toAjax(yhSecurityworkService.updateYhSecuritywork(yhSecuritywork));
    }

    /**
     * 删除保卫工作
     */
    @PreAuthorize("@ss.hasPermi('lys:security:remove')")
    @Log(title = "保卫工作", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(yhSecurityworkService.deleteYhSecurityworkByIds(ids));
    }
}